/**
 * 
 */
package com.ge.healthcare.autosc.ui.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.ui.pojo.UiRequest;
import com.ge.healthcare.autosc.ui.pojo.UiRequestSorter;

/**
 * @author 212042764
 *
 */
public class RequestTypeDUI extends BaseDaoUI {
	public static UserInterfaceDAO uiDAO;
	public static final String RQUEST_TYPE_NAME = "requestTypeName";

	@SuppressWarnings("unchecked")
	public static List<UiRequest> getSwdRequests(String requestTypeName) {
		System.out.println("DAO invoked with requestTypeName:" + requestTypeName);
		List<UiRequest> requests;
		try {
			//String requestTypeName = data.get(RQUEST_TYPE_NAME).toString();
			List<UiRequest> autoscRequests = null;
			autoscRequests = uiDAO.getSwdRequests(requestTypeName);

			if (ASCUtil.isNullOrEmptyList(autoscRequests)) {
				StringBuffer msg = new StringBuffer(
						"No File Records for Query: ");
				msg.append(RQUEST_TYPE_NAME).append(": ").append(requestTypeName).append(" ");
				throw new RuntimeException(msg.toString());
			} else {
				requests = new ArrayList<UiRequest>();
				// Need to sort results
				UiRequestSorter sorter = new UiRequestSorter(autoscRequests);
				Map<Long, List> requestMap = sorter.getMap();
				List<Long> keys = new ArrayList<Long>(sorter.keySet());
				Collections.sort(keys);
				for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
					Long time = (Long) iterator.next();
					if (requestMap.containsKey(time)) {
						List<UiRequest> timeList = requestMap.get(time);
						if (!ASCUtil.isNullOrEmptyList(timeList)) {
							for (UiRequest timeRequest : timeList) {
								requests.add(timeRequest);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			requests = new ArrayList<UiRequest>();
			UiRequest request = new UiRequest();
			setErrorMessage(request, e);
			requests.add(request);
		}
		return requests;
	}

	public void setUiDAO(UserInterfaceDAO uiDAO) {
		RequestTypeDUI.uiDAO = uiDAO;
	}

}
